//
//  DHOrderDetailController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHOrderDetailController.h"
#import "DHNavbarView.h"
#import "DHDetailView.h"
#import <MapKit/MapKit.h>
@interface DHOrderDetailController ()<DHDetailViewDelegate>
@property(nonatomic ,strong)DHNavbarView *navbar;
@property(nonatomic ,strong)DHDetailView *detailView; //详情界面
@end

@implementation DHOrderDetailController

- (void)setRequestInfo:(NSDictionary *)requestInfo{
    _requestInfo = requestInfo;
    
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=cleaning_info&op=getTaskDetail";
    [DHNetworkManager.shareManager requestWithMethod:MethodGet
                                           UrlString:urlStr
                                              params:requestInfo
                                          completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            return ;
        }
        self.detailView.detailModel = [DHDetailModel yy_modelWithJSON:json[@"datas"][@"info"]];
        
    }];
}
// lazy detailView
- (DHDetailView *)detailView{
    if (!_detailView) {
        _detailView = [DHDetailView detailViewWithFrame:CGRectMake(0,
                                                                   CGRectGetMaxY(self.navbar.frame),
                                                                   kScreenWidth,
                                                                kScreenHeight - CGRectGetMaxY(self.navbar.frame))];
        _detailView.delegate = self;
        __weak typeof(self) WeakSelf = self;
        _detailView.completUserInfoClick = ^{
            if (DHNetworkManager.shareManager.userAccount.is_auth.intValue)
            {
                NSMutableDictionary *param = [NSMutableDictionary dictionary];
                param[@"key"] = DHNetworkManager.shareManager.userAccount.key;
                param[@"id"] = WeakSelf.requestInfo[@"id"];
                param[@"store_id"] = WeakSelf.requestInfo[@"store_id"];
                
                [DHNetworkManager.shareManager postTaskWithParam:param completion:^(BOOL isSuccess, NSString *statusInfo)
                 {
                     if (!isSuccess) {
                         [SVProgressHUD showErrorWithStatus:statusInfo];
                         return ;
                     }
                     [SVProgressHUD showSuccessWithStatus:statusInfo];
                 }];

            }else{
                NSLog(@"弹窗");
                DHTipsView *tipView = [DHTipsView tipViewWithFrame:CGRectMake(0, 0, kScreenWidth - 24, 110)];
                STModal *modal = [STModal modal];
                modal.hideWhenTouchOutside = true;
                [modal showContentView:tipView animated:YES];
                
                tipView.completUserInfoClick =
                ^{
                    [modal hide:true];
                    DHUserInfoController *userInfoVC = [DHUserInfoController userInfo];
                    userInfoVC.hidesBottomBarWhenPushed = true;
                    [WeakSelf.navigationController pushViewController:userInfoVC animated:true];
                };
            }
 
        };
    }
    return _detailView;
}
#pragma mark: - 导航按钮代理监听
- (void)detailViewDidnavigationButtonClick:(DHDetailView *)view
{
    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([self.requestInfo[@"lat"] floatValue], [self.requestInfo[@"lng"] floatValue]);
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:loc addressDictionary:nil]];
    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
                                   MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
    
}
// lazy navbar
- (DHNavbarView *)navbar{
    if (!_navbar)
    {
        CGRect frame;
        frame = KIsiPhoneX ? CGRectMake(0, 0, kScreenWidth, 88) : CGRectMake(0, 0, kScreenWidth, 64);
        _navbar = [DHNavbarView navbarViewWithFrame:frame title:@"任务详情" isHiddenReturn:NO];
    }
    return _navbar;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    // 查看评价通知
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(checkEvaList:)
                                               name:kNotificationPushController
                                             object:nil];
    
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(showEvaPicture:)
                                               name:kCommentPhotoBrowerNotification
                                             object:nil];
    
    self.title = @"任务详情";
    
}
#pragma mark: - 查看评价的图片通知监听
- (void)showEvaPicture:(NSNotification *)n
{
    NSArray *urls = n.userInfo[@"imgUrl"];
    NSNumber *idx = n.userInfo[@"imgViewIdx"];
    NSArray<UIImageView *> *ivList = n.userInfo[@"ivList"];

    NSMutableArray<KSPhotoItem *> *items = [NSMutableArray array];
    [ivList enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         KSPhotoItem *item = [KSPhotoItem itemWithSourceView:obj imageUrl:[NSURL URLWithString:urls[idx]]];
         [items addObject:item];
     }];
    KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:idx.integerValue];
    [browser showFromViewController:self];

    
}
- (void)dealloc
{
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:kNotificationPushController
                                                object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:kCommentPhotoBrowerNotification
                                                object:nil];
}
#pragma mark: - 查看全部评价的通知监听
- (void)checkEvaList:(NSNotification *)n
{
   
    DHDetailModel *model = n.userInfo[@"model"];
    
    
    DHEvaluateController *vc = [[DHEvaluateController alloc] init];
    //vc.evaList = evaList;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:true];
}
#pragma mark: - 设置界面
- (void)setupUI{
    __weak typeof(self) WeakSelf = self;
    [self.view setBackgroundColor:UIColor.whiteColor];
    self.navigationController.navigationBar.hidden = true;
    [self.view addSubview:self.navbar];
    self.navbar.popButtonClickBlock =
    ^{
        [WeakSelf.navigationController popViewControllerAnimated:true];
    };
    [self.view addSubview:self.detailView];

}

@end
